package com.tallysystem.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tallysystem.dao.TallysDao;
import com.tallysystem.daoimpl.TallysDaoImpl;
import com.tallysystem.entity.Tallys;

@WebServlet("/doUpdateTally")
public class DoUpdateTallyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取表单提交信息
		Double debit = Double.parseDouble(request.getParameter("debit"));
		String spendtype = request.getParameter("spendtype");
		String remarks = request.getParameter("remarks");
		// string 转成 date
		String spendtime01 = request.getParameter("spendtime");
		String clock01 = request.getParameter("clock");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date01 = new Date();
		Date date02 = new Date();
		try {
			date01 = sdf.parse(spendtime01);
			date02 = sdf.parse(clock01);
		} catch (Exception e) {
			e.printStackTrace();

		}
		java.sql.Date spendtime = new java.sql.Date(date01.getTime());
		java.sql.Date clock = new java.sql.Date(date02.getTime());

		int tid = Integer.parseInt(request.getParameter("tid"));
		String updateReason = request.getParameter("updateReason");

		TallysDao tallyDao = new TallysDaoImpl();
		Tallys tallys = new Tallys(debit, spendtype, remarks, spendtime, clock, tid, null, updateReason);

		int res = tallyDao.updatetallys(tallys);
		if (res >= 0) {
			response.sendRedirect("showTallys");
			System.out.println("修改成功");
		} else {
			response.sendRedirect("error.jsp");
			System.out.println("修改失败");
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
